package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils2.string.IceStringUtils;
import com.edt.ichannel.api.dao.MonitorErrorExpansionDao;
import com.edt.ichannel.api.service.EdtApiService;
import com.edt.ichannel.api.service.MonitorErrorExpansionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class MonitorErrorExpansionServiceimpl implements MonitorErrorExpansionService {
    @Resource
    private MonitorErrorExpansionDao errorExpansionDao;
    @Resource
    private EdtApiService edtApiService;

    @Override
    public void findErrorDataAndSendSms() {
        String onOff = errorExpansionDao.getOnOff();
        if ("on".equals(onOff)) {
            String type = errorExpansionDao.getMonitorType();
            int errorDataCount = 0;
            if ("sql".equals(type)) {
                String sql = errorExpansionDao.getMonitorSql();
                if (IceStringUtils.isNotBlank(sql)) {
                    errorDataCount = errorExpansionDao.excuteMonitorSql(sql);
                }

            } else {
                String glExpansionId = errorExpansionDao.getNotMonitorExpanion();
                errorDataCount = errorExpansionDao.getErrorExpansionCount(glExpansionId);
            }
            if (errorDataCount > 0) {
                String phones = errorExpansionDao.getAdminPhones();
                if (IceStringUtils.isNotBlank(phones)) {
                    String[] phoneArrar = phones.split(",");
                    for (String phone :
                            phoneArrar) {
                        edtApiService.smsSendMessage(phone, "监控有错误账户 数量" + errorDataCount + "，请排查！");
                    }
                    errorExpansionDao.closeTx();
                    errorExpansionDao.setOff();
                }
            }
        }

    }
}
